package net.mingsoft.report_data.biz.impl.report_gen;

import com.alibaba.fastjson.JSON;
import com.baomidou.dynamic.datasource.annotation.DS;
import net.mingsoft.report_data.biz.ZkDataEnterpriseActivityOfEconomyBiz;
import net.mingsoft.report_data.dao.ZkTgdDao;
import net.mingsoft.report_data.entity.SmuInfoRecordEntity;
import net.mingsoft.report_data.util.MathUtil;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.helpers.MessageFormatter;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author gengfa
 * @date 22/05/19
 */
@Service
@DS(value = "tdg")
public class ZkDataEnterpriseActivityOfEconomyBizImpl implements ZkDataEnterpriseActivityOfEconomyBiz {

    @Resource
    ZkTgdDao tgdDao;

    private static final String DESC_FREE_TAX_ILLEGAL_SALE = "据统计，近一周的免税商品违规售卖案件数量累计共有{}条，具体情况如下表所示。";
    private static final String DESC_FREE_TAX_INTERNET_SALE = "对免税商品售卖高发渠道进行统计分析，由下表可得，以{}渠道售卖居多，其中{}销售数量最多，{}其次。{}销售数量{}，占比{}%，{}销售数量{}，占比{}%。";
    private static final String DESC_FREE_TAX_SMU_AREA = "据统计，近一周岛内走私高发地区前三名分别为{}，具体排名情况如下表所示。";

    @Override
    public Map<String, Object> getFreeTaxIllegalSale(Map<String, Object> map) {
        final String beginDate = (String) map.get("beginDate");
        final String endDate = (String) map.get("endDate");
        List<SmuInfoRecordEntity> freeTaxIllegalSaleList = tgdDao.selectPage(beginDate, endDate, 10);
        long total = freeTaxIllegalSaleList.size();
        map.put("freeTaxIllegalSaleList", JSON.toJSONString(freeTaxIllegalSaleList));
        map.put("freeTaxIllegalSaleDesc", MessageFormatter.format(DESC_FREE_TAX_ILLEGAL_SALE, String.valueOf(total)).getMessage());
        return map;
    }

    @Override
    public Map<String, Object> getFreeTaxInternetSale(Map<String, Object> map) {
        final String beginDate = (String) map.get("beginDate");
        final String endDate = (String) map.get("endDate");
        List<SmuInfoRecordEntity> freeTaxInternetSaleList = tgdDao.selectGroupByGoodSource(beginDate, endDate, 10);
        long total = freeTaxInternetSaleList.size();
        long totalGoodsNum = freeTaxInternetSaleList.stream().mapToLong(SmuInfoRecordEntity::getGoodsNum).sum();
        for (SmuInfoRecordEntity entity : freeTaxInternetSaleList) {
            entity.setHeadUrl(MathUtil.getRate(entity.getGoodsNum(), totalGoodsNum));
        }
        if (total >= 3) {
            String message = MessageFormatter.arrayFormat(DESC_FREE_TAX_INTERNET_SALE, new String[]{
                    freeTaxInternetSaleList.stream().limit(3).map(SmuInfoRecordEntity::getSourceType).collect(Collectors.joining("、")),
                    freeTaxInternetSaleList.get(0).getSourceType(),
                    freeTaxInternetSaleList.get(1).getSourceType(),
                    freeTaxInternetSaleList.get(0).getSourceType(),
                    String.valueOf(freeTaxInternetSaleList.get(0).getGoodsNum()),
                    totalGoodsNum==0?"0":MathUtil.getRate(freeTaxInternetSaleList.get(0).getGoodsNum(), totalGoodsNum),
                    freeTaxInternetSaleList.get(1).getSourceType(),
                    String.valueOf(freeTaxInternetSaleList.get(1).getGoodsNum()),
                    totalGoodsNum==0?"0":MathUtil.getRate(freeTaxInternetSaleList.get(1).getGoodsNum(), totalGoodsNum)
            }).getMessage();
            map.put("freeTaxInternetSaleDesc", message);
        } else {
            map.put("freeTaxInternetSaleDesc", "");
        }
        map.put("freeTaxInternetSaleList", JSON.toJSONString(freeTaxInternetSaleList));
        return map;
    }

    @Override
    public Map<String, Object> getFreeTaxSmugglingArea(Map<String, Object> map) {
        final String beginDate = (String) map.get("beginDate");
        final String endDate = (String) map.get("endDate");
        List<SmuInfoRecordEntity> freeTaxSmuAreaList = tgdDao.selectGroupByArea(beginDate, endDate, 10);
        map.put("freeTaxSmuAreaList", JSON.toJSONString(freeTaxSmuAreaList));
        map.put("freeTaxSmuAreaDesc", MessageFormatter.format(DESC_FREE_TAX_SMU_AREA,
                freeTaxSmuAreaList.stream().limit(3).map(SmuInfoRecordEntity::getPublishedArea).collect(Collectors.joining("、"))).getMessage());
        return map;
    }

    /**
     * please check {@link ZkDataSgSqBizImpl#getPopulationFlowAndEconomicMetric}
     */
    @Override
    public Map<String, Object> getPopulationFlowAndEconomicMetric(Map<String, Object> map) {
        final String beginDate = (String) map.get("beginDate");
        final String endDate = (String) map.get("endDate");
        return map;
    }
}
